package com.upc.feedish.utils;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.upc.feedish.R;

import java.util.List;
import java.util.Map;

public class DeviceViewAdapter extends BaseAdapter {

    private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;
    private ImageView gou;
    static int selectItem=-1;

    public DeviceViewAdapter(Context context, List<Map<String, Object>> data) {
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context);

    }

    //组件集合，对应list.xml中的控件
    public final class Module {
        public TextView title;
        public TextView info;
        public Button view;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Module module;
        if (convertView == null) {
            module = new Module();
            //获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.device_item, null);
            module.title = convertView.findViewById(R.id.title);
            module.info = convertView.findViewById(R.id.info);
            convertView.setTag(module);
        } else {
            module = (Module) convertView.getTag();
        }
        System.out.println("p3:"+position+",,,p4:"+selectItem);
        if(position==selectItem){
            //如果第position位置的数据为当前点击选中的位置则使用R.drawable.cv_press背景
            convertView.setBackgroundResource(R.drawable.cv_press);
            gou = convertView.findViewById(R.id.gou);
            gou.setVisibility(View.VISIBLE);
        }else{
            ///如果第position位置的数据不是当前选中的位置则使用纯白色作为背景
            gou = convertView.findViewById(R.id.gou);
            gou.setVisibility(View.INVISIBLE);
            convertView.setBackgroundColor(Color.WHITE);
        }

        //绑定数据

        module.title.setText((String) data.get(position).get("name"));
        module.info.setText("设备ID："+(String) data.get(position).get("deviceId").toString());
        return convertView;
    }

    //获得被选中的item的位置
    public void setSelectItem( int position){
        this.selectItem = position;
    }


}
